// pages/test/test.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mode:true,
    limit:true,
    userInfo:{},
    userDetail:{},
    app:getApp(),
    options:{},
    historyToday:[],
    passToday:0,
    missToday:0,
    historyWeek:[],
    passWeek:0,
    missWeek:0,
  },

  changeModeLim(){
    this.setData({
      mode:false,
      limit:true
    })
  },
  changeModeUnl() {
    this.setData({
      mode:false,
      limit:false
    })
  },
  changeModeDef(){
    this.setData({
      mode:true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({
        options:options
      })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: '自我挑战'
    });
    
    this.setData({
      userInfo:getApp().data.userInfo,
      userDetail:getApp().data.userDetail,
      mode: this.data.options.mode ? this.data.options.mode>0:true,
      limit: this.data.options.limit ? this.data.options.limit>0:true
    })
    const date=new Date();
    getApp().getUserHistory({
      UUID:this.data.userInfo.openId,
      startTime:`${date.getFullYear()}-${("0"+(date.getMonth()+1).toString()).substr(-2)}-${("0"+date.getDate()).substr(-2)}T00:00:00.000Z`
    },(res)=>{
      // console.log(res);
      let s=0;
      for(const i of res){
        if(i.result===0) s++;
      }
      this.setData({
        passToday:res.length,
        missToday:s
      })
    })
    let weekday=new Date();
    weekday.setTime(weekday.getTime() - (weekday.getDay() - 1) * 1000 * 60 * 60 * 24)
    getApp().getUserHistory({
      UUID:this.data.userInfo.openId,
      startTime:`${weekday.toISOString().substr(0,11)}00:00:00.000Z`
    },res=>{
      // console.log(res);
      let s=0;
      for (const i of res) {
        if (i.result === 0) s++;
      }
      this.setData({
        passWeek: res.length,
        missWeek: s
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    wx.setNavigationBarTitle({
      title: ''
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})